package com.yiqi.lottery.feature.login.view;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.lottery.R;
import com.yiqi.lottery.feature.login.contract.RegisterContract;
import com.yiqi.lottery.feature.login.dependencies.register.DaggerRegisterComponent;
import com.yiqi.lottery.feature.login.dependencies.register.RegisterPresenterModule;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

public class RegisterActivity extends BaseActivity implements RegisterContract.View {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @Inject
    RegisterContract.Presenter mPresenter;
    @BindView(R.id.edt_user_register_username)
    PowerfulEditText mEdtUserRegisterUsername;
    @BindView(R.id.edt_user_register_password)
    PowerfulEditText mEdtUserRegisterPassword;
    @BindView(R.id.edt_user_register_invite_code)
    PowerfulEditText mEdtUserRegisterInviteCode;
    @BindView(R.id.edt_user_register_mobile)
    PowerfulEditText mEdtUserRegisterMobile;
    @BindView(R.id.edt_user_register_code)
    PowerfulEditText mEdtUserRegisterCode;
    @BindView(R.id.btn_get_code)
    TextView mBtnGetCode;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_register;
    }

    @Override
    protected void initInjector() {
        DaggerRegisterComponent.builder().registerPresenterModule(new RegisterPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "用户注册", true, true);
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.toolbar, R.id.btn_user_register_scan, R.id.btn_get_code, R.id.btn_user_register, R.id.btn_user_register_had_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_user_register_scan:
                break;
            case R.id.btn_get_code:
                mPresenter.doUserRegisterSendCode(mEdtUserRegisterMobile.getText().toString());
                break;
            case R.id.btn_user_register:
                mPresenter.doUserRegister(mEdtUserRegisterMobile.getText().toString(),
                        mEdtUserRegisterUsername.getText().toString(),
                        mEdtUserRegisterCode.getText().toString(),
                        mEdtUserRegisterPassword.getText().toString(),
                        mEdtUserRegisterInviteCode.getText().toString());
                break;
            case R.id.btn_user_register_had_account:
                finish();
                break;
        }
    }


    @Override
    public void showRegisterSuccess() {
        T.showShort("注册成功！");
        finish();
    }

    @Override
    public void showRegisterError(String error) {
        T.showShort(error);
    }

    @Override
    public void showSendCodeSuccess(String code) {
        T.showShort("获取验证码成功!");
        onGetCode();
    }

    @Override
    public void showSendCodeError(String error) {
        T.showShort(error);
    }

    @Override
    public void showResetSuccess(String userInfoBo) {

    }

    @Override
    public void showResetError(String error) {

    }

    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnGetCode.setEnabled(false);
                        mBtnGetCode.setTextColor(AM.res().getColor(R.color.text_secondary));
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnGetCode.setText( num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnGetCode.setEnabled(true);
                mBtnGetCode.setText("发送验证码");
                mBtnGetCode.setTextColor(AM.res().getColor(R.color.primary_highlight));
            }
        });

    }

}